----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
      name:  <unnamed>
       log:  F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files/code/logs/model_07_selection.log
  log type:  text
 opened on:  18 Nov 2022, 21:33:54

. *-------------------------------------------------------------------------------
. 
. 
. 
. local y=2016

. local ctryrel "FR"

. local dd=10

. local lev "sector" 

. 
. 
. *(1) Firm-sector FE (to create deciles based on this) 
. *-----------------------------------------
. clear all

. use "${data}/firmFE_naics_sales_s1_base_woparent.dta", clear

. keep if year==`y' 
(0 observations deleted)

. replace firm_FE=firm_FE
(0 real changes made)

. keep year type hq guo_bvd firm_FE sector

. duplicates drop 

Duplicates in terms of all variables

(0 observations are duplicates)

. egen decile_guo_FE = xtile(firm_FE), by(`lev' type year) n(`dd')

. sum decile_guo_FE

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
decile_guo~E |     13,073    5.494607    2.872919          1         10

. tempfile decile_guo_FE

. save `decile_guo_FE', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000o.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000o.tmp saved

. 
. 
. 
. *(2) Measure by country-sector (by Z) 
. *--------------------------------------------
. local grvar0 "LP_ppp_emp_pwt" 

. 
. use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear

. drop if sector1==sector
(0 observations deleted)

. replace DA=. if num_aff<3
(31 real changes made, 31 to missing)

. replace DP=. if num_aff<3
(0 real changes made)

. 
. keep if year==`y' 
(0 observations deleted)

. rename DA D_A

. rename DP D_P

. drop if D_A==. | (D_A==0 & isocode!="FR")
(32 observations deleted)

. 
. local var "D_A"

. merge m:1 year isocode sector using "${data}/aggregates_tfp_lp_klems.dta" 
(note: variable sector was str26, now str37 to accommodate using data's values)

    Result                           # of obs.
    -----------------------------------------
    not matched                        12,067
        from master                         0  (_merge==1)
        from using                     12,067  (_merge==2)

    matched                               445  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(12,067 observations deleted)

. drop _merge

. 
. foreach var in D_A  {
  2. gen `var'_phi=(-1)*`var'*(0.2)
  3. *---------------------------------------
. gen b_`var'_phi_`grvar0'=ln_`grvar0' - `var'_phi
  4. }

. 
. gen Z=b_D_A_phi_`grvar0'

. egen decile_Z = xtile(Z), by(type `lev' year) n(`dd')

. by type `lev' year, sort: egen num_ctry=count(Z)

. gsort type year Z

. by type year, sort: gen D_Waugh_rank=_n

. rename D_A A

. keep isocode type `lev' year A D_Waugh_rank R2 R2adj R2w R2wadj decile_Z* 

. tempfile decile_Z

. save `decile_Z', replace 
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000p.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000p.tmp saved

. 
. 
. 
. 
. *(3) Measure by country-sector (by popularity, number of foreign affiliates operating in each country-sector pair) 
. *--------------------------------------------
. use isocode sector1 sector year GO_usd GO_usd_exp *ENT_m* using "${data}/klems_oecd_unido_orbis_sales_emp_exp.dta", clear

. keep if isocode!=""
(0 observations deleted)

. keep if isocode=="DK" | isocode=="JP" | isocode=="IT" | isocode=="KR" | isocode=="DE" | isocode=="FR" | isocode=="ES" |  isocode=="MX" |  isocode=="GB" |  isocode=="PL" |  isoc
> ode=="GR" |  isocode=="NL" |  isocode=="RO" |  isocode=="BE" |  isocode=="AT" |  isocode=="PT" |  isocode=="FI" |  isocode=="SE" |  isocode=="CZ" |  isocode=="SK" |  isocode=="
> BG" |  isocode=="HU" |  isocode=="HR" |  isocode=="LV" |  isocode=="SI" |  isocode=="LT" |  isocode=="EE" 
(4,095 observations deleted)

. keep if sector1!=sector
(2,106 observations deleted)

. keep if year==`y'
(6,156 observations deleted)

. 
. gen type=""
(513 missing values generated)

. replace type="manuf" if sector1=="Manufacturing (C)"
variable type was str1 now str5
(162 real changes made)

. replace type="serv" if sector1=="Market_Services (G-H-I-J-K-M-N-R-S-T)" 
(162 real changes made)

. replace type="nonmarket" if sector1=="Non-Market Economy"
variable type was str5 now str9
(108 real changes made)

. replace type="others" if sector1=="Other_Goods (A-B-D-E-F)"
(81 real changes made)

. order year isocode sector

. 
. gen ENT_mnc2=ENT_mnc
(109 missing values generated)

. egen decile_numaff = xtile(ENT_mnc2), by(type `lev' year) n(`dd')
(109 missing values generated)

. drop if decile_numaff==.
(109 observations deleted)

. tempfile decile_popularity

. save `decile_popularity', replace 
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000q.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000q.tmp saved

. 
. 
. 
. *Bring this deciles to the residuals
. *--------------------------------------------
. clear all

. use "${data}/residual_naics_sales_s1_base_woparent.dta", clear

. keep if year==`y' 
(0 observations deleted)

. $whichtypes

. tab type

       type |      Freq.     Percent        Cum.
------------+-----------------------------------
      manuf |     10,996       22.08       22.08
  nonmarket |      2,227        4.47       26.55
     others |      1,974        3.96       30.51
       serv |     34,614       69.49      100.00
------------+-----------------------------------
      Total |     49,811      100.00

. 
. merge m:1 isocode type `lev' using `decile_popularity', keepusing(decile_*)
(note: variable sector was str26, now str37 to accommodate using data's values)

    Result                           # of obs.
    -----------------------------------------
    not matched                         1,382
        from master                     1,378  (_merge==1)
        from using                          4  (_merge==2)

    matched                            48,433  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(4 observations deleted)

. drop _merge

. 
. merge m:1 isocode type `lev' year using `decile_Z', keepusing(decile_*)

    Result                           # of obs.
    -----------------------------------------
    not matched                            42
        from master                        42  (_merge==1)
        from using                          0  (_merge==2)

    matched                            49,769  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(0 observations deleted)

. drop _merge

. 
. merge m:1 type sector  year hq guo_bvd using `decile_guo_FE', keepusing(decile_*)

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                            49,811  (_merge==3)
    -----------------------------------------

. drop if _merge==2
(0 observations deleted)

. drop _merge

. 
. rename decile_Z dec_Z

. rename decile_numaff dec_numaff

. rename decile_guo_FE dec_guoFE

. 
. tempfile base0 

. save `base0', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000r.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000r.tmp saved

. 
. 
. 
. 
. *Calculate the mean across all firms within a decile across sectors (Number of fimrs)
. *--------------------------------------------
. foreach ff in  dec_guoFE  { 
  2. foreach xx in dec_Z dec_numaff {
  3. display "`ff' / `xx'"
  4. 
. use `base0', clear
  5. egen resid_sd=sd(lhs2)
  6. gen `ff'_`xx'=resid/resid_sd
  7. gen a=1
  8. collapse (mean) lhs2 xb1 xbd1 resid* `ff'_`xx'  (count) num_firm=a, by(`xx' `ff' year)
  9. 
. rename `ff' dec_firmsec
 10. rename `xx' dec_ctrysec
 11. keep if dec_firmsec!=. & dec_ctrysec!=.
 12. 
. tempfile `ff'_`xx'_s
 13. save ``ff'_`xx'_s', replace
 14. }
 15. }
dec_guoFE / dec_Z
(10 observations deleted)
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000s.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000s.tmp saved
dec_guoFE / dec_numaff
(10 observations deleted)
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000t.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000t.tmp saved

. *
. 
. use `dec_guoFE_dec_Z_s', clear

. foreach ff in  dec_guoFE  { 
  2. foreach xx in dec_numaff {
  3. display "`ff' / `xx'"
  4. merge 1:1 dec_firmsec dec_ctrysec using ``ff'_`xx'_s'
  5. des
  6. drop _merge
  7. }
  8. }
dec_guoFE / dec_numaff

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                               100  (_merge==3)
    -----------------------------------------

Contains data from C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000s.tmp
  obs:           100                          
 vars:            12                          18 Nov 2022 21:33
                                              (_dta has notes)
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
year            int     %9.0g                 year of observation regarding emp/turn
dec_ctrysec     float   %9.0g                 
dec_firmsec     float   %9.0g                 
lhs2            float   %9.0g                 (mean) lhs2
xb1             double  %10.0g                (mean) xb1
xbd1            double  %10.0g                (mean) xbd1
resid           double  %10.0g                (mean) resid
resid_sd        float   %9.0g                 (mean) resid_sd
dec_guoFE_dec_Z float   %9.0g                 (mean) dec_guoFE_dec_Z
num_firm        long    %9.0g                 (count) a
dec_guoFE_dec~f float   %9.0g                 (mean) dec_guoFE_dec_numaff
_merge          byte    %23.0g     _merge     
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Sorted by: dec_firmsec  dec_ctrysec
     Note: Dataset has changed since last saved.

. tempfile final

. save `final', replace
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000u.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000u.tmp saved

. rename dec_ctrysec ctrysec

. rename dec_firmsec firmsec

. 
. use `final', clear

. foreach var of varlist dec_* {
  2.         local newname : subinstr local var "dec_" ""
  3.         rename `var' `newname'
  4. }

. foreach var of varlist guo* {
  2.         local newname : subinstr local var "dec_" ""
  3.         rename `var' `newname'
  4. }

. *
. save "${data}/selection_tomatlab_type_`dd'_agg_woparent.dta", replace 
(note: file F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files/data/analysis/selection_tomatlab_type_10_agg_wop
> arent.dta not found)
file F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files/data/analysis/selection_tomatlab_type_10_agg_woparent.d
> ta saved

. 
. local llb guoFE_Z 

. local lld guoFE_numaff 

. local ll `llb'  `lld'

. 
. display "`ll'"
guoFE_Z  guoFE_numaff

. 
. 
. *Prepare final data for figure 7
. *=================================================
. foreach v of local ll {
  2. use "${data}/selection_tomatlab_type_`dd'_agg_woparent.dta", clear
  3. keep ctrysec firmsec `v'
  4. reshape wide guo*, i(ctrysec) j(firmsec)
  5. tempfile `v'
  6. save ``v'', replace
  7. }
(note: j = 1 2 3 4 5 6 7 8 9 10)

Data                               long   ->   wide
-----------------------------------------------------------------------------
Number of obs.                      100   ->      10
Number of variables                   3   ->      11
j variable (10 values)          firmsec   ->   (dropped)
xij variables:
                                guoFE_Z   ->   guoFE_Z1 guoFE_Z2 ... guoFE_Z10
-----------------------------------------------------------------------------
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000v.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000v.tmp saved
(note: j = 1 2 3 4 5 6 7 8 9 10)

Data                               long   ->   wide
-----------------------------------------------------------------------------
Number of obs.                      100   ->      10
Number of variables                   3   ->      11
j variable (10 values)          firmsec   ->   (dropped)
xij variables:
                           guoFE_numaff   ->   guoFE_numaff1 guoFE_numaff2 ... guoFE_numaff10
-----------------------------------------------------------------------------
(note: file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000w.tmp not found)
file C:\Users\VANESS~1\AppData\Local\Temp\ST_324c_00000w.tmp saved

. *
. clear all

. use `guoFE_Z', clear

. foreach v of local ll {
  2. merge 1:1 ctrysec using ``v''   
  3. drop _merge
  4. }

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                                10  (_merge==3)
    -----------------------------------------

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                                10  (_merge==3)
    -----------------------------------------

. export delimited ctrysec guo*  using "${data}/selection_tomatlab_type_`dd'_agg_woparent.csv", replace
(note: file F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files/data/analysis/selection_tomatlab_type_10_agg_wop
> arent.csv not found)
file F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files/data/analysis/selection_tomatlab_type_10_agg_woparent.c
> sv saved

. **Next go to Matlab**
. 
. 
end of do-file

. do "F:\Dropbox (UBC-Umich)\Research_Projects\Project Javier and Natalia\TFP firm\TFP_firm_data_historical\dataverse_files\code\appendix\table_A2_elasticity.do"

. /***************************************************************************************
> Firm-embedded productivity and cross-country income differences
> Alviarez, Cravino and Ramondo
> Journal of Political Economy (2022)
> 
> Program: table_01_elasticity.do
> Date: October 2022
> 
> Description: Reproduces Table 1: Estimating the composite elasticity, \beta.
> 
> *****************************************************************************************/
. 
. *-------------------------------------------------------------------------------
. global typeden=1

. include "set_directories.do"

. /***************************************************************************************
> Firm-embedded productivity and cross-country income differences
> Alviarez, Cravino and Ramondo
> Journal of Political Economy (2022)
> 
> Program: set_directories.do
> Date: October 2022
> 
> Description: Sets directory paths
> *****************************************************************************************/
. 
. *Set root directory here
. global root "F:/Dropbox (UBC-Umich)/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical/dataverse_files" 

. 
. 
. *All other directories set automatically
. 
. *Code
. global code "${root}/code"

. global clogs "${root}/code/logs"

. global cmain "${root}/code/main"

. global cappendix "${root}/code/appendix"

. 
. *Data
. global data "${root}/data/analysis"

. global tmp  "${root}/data/tmp"

. 
. *Results
. global rmain "${root}/results/main"

. global rappendix  "${root}/results/appendix"

. 
. if $typeden==0 {
. local denominator "_exp_ko"
. }

. if $typeden==1 {
. local denominator "_ko"
. }

. *
. 
. 
. 
. /*******************************************************************************
> *Stata packages to install:
>    1) gtools
>    2) reghdfe
> *******************************************************************************/
. 
. 
. ************** ESTO DE ACA ES LO QUE TENGO QUE BORRAR **************
. *global maindirectory "F:/Dropbox (UBC-Umich)"
. *global maindirectory "C:\Users\cheoy\Dropbox (UBC-Umich)"
. *global tfp "${maindirectory}/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data_historical" 
. *global tfp_bef "${maindirectory}/Research_Projects/Project Javier and Natalia/TFP firm/TFP_firm_data" 
. *global orbis_historical "${maindirectory}/Master_Dataset/Orbis_master/Javier_Florian/orbis_data/final_data"
. ************** ESTO DE ACA ES LO QUE TENGO QUE BORRAR **************
. 
. *global inputs "${tfp}/data/inputs"
. *global code "${tfp}/code"
. *global output "${tfp}/data/output"
. *global results "${tfp}/results"
. 
. 
. 
. 
. 
. 
. 
. 
. set memory 64g
set memory ignored.
    Memory no longer needs to be set in modern Statas; memory adjustments are performed on the fly automatically.

. global lf "LF"

. 
. *Log
. cap log close
